package com.hdoit.kylin.web.experiment.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.experiment.model.Experiment;
import com.hdoit.kylin.logical.experiment.service.IExperimentService;
import com.hdoit.kylin.logical.group.service.IProjectService;
import com.hdoit.kylin.logical.relative.service.IProjectUserService;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherStudentService;
import com.hdoit.kylin.logical.viewmodel.studentManager.ExperimentSearch;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;
import com.hdoit.kylin.web.common.CommonWebUtils;


/**
 * @author luozhicheng
 * @email
 * @createtime 2017年07月19日 17:34:01
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/experiment/")
public class ExperimentController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/experiment/";
	}

	private IExperimentService experimentService;

	public IExperimentService getExperimentService() {
		return experimentService;
	}

	@Resource
	public void setExperimentService(IExperimentService experimentService) {
		this.experimentService = experimentService;
	}
	
	private IStudentService userService;
	
	public IStudentService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IStudentService userService) {
		this.userService = userService;
	}

	private IProjectService projectService;
	
	public IProjectService getProjectService() {
		return projectService;
	}
	@Resource
	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}

	private IProjectUserService project_userService;
	

	public IProjectUserService getProject_userService() {
		return project_userService;
	}
	
	@Resource
	public void setProject_userService(IProjectUserService project_userService) {
		this.project_userService = project_userService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "experimentDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";
	//private final String dutyUsersViewName = "dutyUsers";
	private final String queryListByProjectIdViewName = "queryListByProjectId";


	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,
			ExperimentSearch search, boolean ajaxPost) {
		List<String> employeeIds = new ArrayList<String>();
		employeeIds.add(getLoginModel().getLoginId());
		
		//教师查询学生
		if(CommonWebUtils.isTeacher(getLoginModel())){
			ITeacherStudentService teacherStudentService = CommonApplicationContextUtil.getCurrentContext().getBean(ITeacherStudentService.class);
			TeacherStudentSearch teacherStudentSearch = new TeacherStudentSearch();
			teacherStudentSearch.setTeacherId(getLoginModel().getLoginId());
			teacherStudentSearch.setGraduateMark(YesOrNoInteger.no);
			List<TeacherStudent> students = teacherStudentService.queryList(teacherStudentSearch);
			for(TeacherStudent student:students){
				employeeIds.add(student.getStudentId());
			}
		}
		
		search.setEmployeeIds(employeeIds);
		pagerModel.setTotalAmount(experimentService.queryCount(search));
		List<Experiment> experiments = experimentService.queryList(search,
				pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("experiments", experiments);
		model.addAttribute("experimentSearch", search);
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	


	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String experimentDetailed(Model model, String key) {
		Experiment experiment = new Experiment();
		if (!StringUtils.isEmpty(key)) {
			experiment = experimentService.queryModel(key);
		}
		model.addAttribute("experiment", experiment);
		return view(detailedViewName);
	}

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, Experiment experiment) {
		if (StringUtils.isEmpty(experiment.getExperimentId())) {
			experiment.setExperimentId(CommonUtils.getKeyId());
//			experiment.setExperimentComment(experiment.getExperimentComment());
//			experiment.setExperimentAddress(experiment.getExperimentAddress());
//			experiment.setExperimentDate(experiment.getExperimentDate());
//			experiment.setExperimentName(experiment.getExperimentName());
			experiment.setEmployeeId(getLoginModel().getLoginId());
		//	experiment.setProjectId(experiment.getProjectId());
			experiment.setModelStatus(ModelStatus.add);
		} else {

			experiment.setModelStatus(ModelStatus.update);
		}
		ActionResult actionResult;
		if (experimentService.applyChange(experiment)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		model.addAttribute("experiment", experiment);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (experimentService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
	
	 @ResponseBody
	    @RequestMapping(value =queryListByProjectIdViewName, method = { RequestMethod.POST },produces = "text/json;charset=UTF-8")
	    public String queryListByDbname(String projectId){
	    	List<Experiment> experiments=experimentService.queryListByProjectId(projectId);
	    	System.out.println(experiments.get(0).getExperimentName()+"2222233");
	    	
	    	return JsonUtils.writeValueAsString(JsonUtils.list2JSONObject(experiments, "experimentId","experimentName"));
	    }
//	 @RequestMapping(value =dutyUsersViewName, method = { RequestMethod.POST,RequestMethod.GET },produces="text/plain;charset=utf-8")
//	    public @ResponseBody String dutyUsers(String projectId){
//	    	List<String> userIds=project_userService.queryListByProjectId(projectId);
//	    	List<Student> users= userService.queryList(userIds);
//	    	List<ListItem> items=employee2ListItem(users);
//	    	
//	    	return JsonUtils.writeValueAsString(items);
//	    }
//	    
//	    private List<ListItem> employee2ListItem(List<Student> users){
//	    	if(users==null){return null;}
//	    	List<ListItem> items=new ArrayList<ListItem>();
//	    	ListItem item=null;
//	    	for (Student user : users) {
//				item=new ListItem();
//				item.setText(user.getEmployeeName());
//				item.setValue(user.getEmployeeId());
//				
//				items.add(item);
//			}
//	    	return items;
//	    }
}
